/**
* @description: 危险点分析
* @author: wqn - yanxiaoyue
* @data:2023年6月29日 - 2023年7月13日
* @remark:
**/

<template>
	<view class="danger">
		<nav-bar title="危险点分析" @goBack="goBack"></nav-bar>
		<view class="main">
			<view class="title" @click="changeAll">
				<text>危险点分析及预控措施</text>
				<van-checkbox v-model="isCheckAll">
					<template #icon="props">
						<img class="img-icon" :src="props.checked ? inactiveIcon : activeIcon" />
					</template>
				</van-checkbox>
				<text :class="isCheckAll ? 'active' : ''">全选</text>
			</view>
			<view class="content-list" v-for="(item,index) in dangerData" :key="index">
				<view class="border">
					<view class="sub-title" @click="handleChecked(item)">
						<span>{{ index + 1 }}</span>
						<text>{{ item.hazardPointAnalysis }}</text>
						<van-checkbox shape="square" v-model="item.checked">
							<template #icon="props">
								<img class="img-icon" :src="props.checked ? inactiveIcon : activeIcon" />
							</template>
						</van-checkbox>
						<!-- <van-checkbox-group>
              
            </van-checkbox-group> -->
					</view>
					<van-divider></van-divider>
					<view class="sub-content">
						<text>预控措施：</text>
						<br>
						<text>{{ item.controlMeasure }}</text>
					</view>
				</view>
			</view>
		</view>
		<!--      确认按钮-->
		<view class="verify">
			<van-button type="info" round @click="affirmClick">确认</van-button>
		</view>
	</view>
</template>

<script>
	import NavBar from "../../../standardizedOperation/common/navBar.vue";
	import {
		Dialog,
		Toast
	} from "vant";
	import {
		insertwxdmeasureslog,
		selectwxdmeasures
	} from "../../../../api/bzhzyApi";

	export default {
		name: "dangerDetails",
		components: {
			NavBar
		},
		data() {
			return {
				isCheckAll: false, // 是否全选
				allCheck: 0,
				activeIcon: require('../../../../static/img/no-click.png'),
				inactiveIcon: require('../../../../static/img/clicked.png'),
				dangerData: []
			}
		},
		onLoad(option) {
			this.work = JSON.parse(option.message)
			this.getWxdmeasures()
		},
		methods: {
			goBack() {
				uni.navigateBack()
			},
			/**
			 * 通知上一个页面刷新数据
			 */
			back() {
				let pages = getCurrentPages();
				let prevPage = pages[pages.length - 2]; //上一个页面
				prevPage.isRefresh = true
				uni.navigateBack()
			},
			//   查询所有危险措施
			getWxdmeasures() {
				this.$apiRequest.bzhzyApi.selectwxdmeasures({
					consultationDeductionId: this.work.OBJID
				}).then(res => {
					res.data.forEach(function(item) {
						'wxdMeasures' in item ? item.checked = true : item.checked = false
					})
					this.dangerData = res.data;
					let CheckedAllArr = [];
					CheckedAllArr = this.dangerData.filter(item => item.checked == true);
					this.allCheck = CheckedAllArr.length;
					this.allCheck === this.dangerData.length ? this.isCheckAll = true : this.isCheckAll = false;
					console.log(this.dangerData, '修改后的数据');
				})
			},
			//全选
			changeAll() {
				this.isCheckAll = !this.isCheckAll;
				this.isCheckAll ? this.dangerData.forEach(function(item) {
					item.checked = true;
				}) : this.dangerData.forEach(function(item) {
					item.checked = false;
				})
			},
			//单选
			handleChecked(item) {
				let CheckedAllArr = [];
				CheckedAllArr = this.dangerData.filter(item => item.checked == true);
				this.allCheck = CheckedAllArr.length;
				item.checked = !item.checked;
				item.checked ? this.allCheck++ : this.allCheck--
				this.allCheck === this.dangerData.length ? this.isCheckAll = true : this.isCheckAll = false;
			},
			//提交
			affirmClick() {
				let CheckedAllArr = [];
				this.dangerData.filter(function(item) {
					if (item.checked === true) {
						CheckedAllArr.push(item.objId)
					}
				});
				Dialog.confirm({
						message: '提交后不可修改，是否确定提交？',
						confirmButtonColor: "#417adc",
					})
					.then(() => {
						// on confirm
						this.$apiRequest.bzhzyApi.insertwxdmeasureslog({
							workPlanId: this.work.id,
							consultationDeductionId: this.work.OBJID,
							tFlowContentId: this.work.flowId,
							array: CheckedAllArr
						}).then(res => {
							Toast(res.msg)
							Toast.success(res.msg);
							this.back();
						}).catch(error =>{
							Toast("提交失败")
						})
					})
					.catch(() => {
						// on cancel
					});
			},

		}
	}
</script>

<style lang="scss" scoped>
	.danger {
		display: flex;
		flex-direction: column;
		width: 100vw;
		height: 100vh;
		background: #F2F6FA;
		overflow: hidden;

		.main {
			flex: 1;
			overflow: scroll;
			margin: px2vh(50) px2vw(30);

			text {
				font-size: px2vh(40);
				font-weight: bold;
				color: #666666;
			}

			.title {
				margin-bottom: px2vh(30);
				display: flex;
				align-items: center;

				text:nth-child(1) {
					flex: 1
				}

				img {
					margin-right: px2vw(16);
				}
			}

			.content-list {
				background: #FFFFFF;
				border-radius: px2vh(20);
				overflow: hidden;
				margin-bottom: px2vh(30);

				.border {
					margin: px2vh(50);

					.sub-title {
						background: #FFFFFF;
						display: flex;
						align-items: center;

						.active {
							color: #007AFF;
						}

						span {
							display: block;
							width: 18px;
							height: 18px;
							background: #417ADC;
							border-radius: 100%;
							color: #FFFFFF;
							font-weight: bold;
							text-align: center;
							margin-right: px2vw(22);
						}

						text {
							flex: 1;
							font-size: px2vh(42);
						}

					}

					.sub-content {
						text {
							font-size: px2vh(38);
							font-weight: normal;
							line-height: px2vh(62);
						}
					}
				}

			}

		}

		.verify {
			width: 100%;
			padding: 0 px2vh(30) px2vh(30) px2vh(30);
			box-sizing: border-box;

			.van-button--normal {
				width: 100%;
				height: px2vh(120);
				font-size: px2vh(46);
				background: #417ADC;
				border: 1px solid #417ADC;
			}
		}
	}

	.img-icon {
		width: 13px;
		height: 13px;
	}
</style>
